import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qaun_ni_dev/pages/report/report_detail.dart';
import 'package:qaun_ni_dev/provider/report_provider.dart';


class ReportPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('举报审核列表'),
      ),
      body: ReportListPage(),
    );
  }
}

class ReportListPage extends StatefulWidget {
  @override
  _ReportListPageState createState() => _ReportListPageState();
}

class _ReportListPageState extends State<ReportListPage> {

  GlobalKey<RefreshHeaderState> _headerKey44 = new GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey44 = new GlobalKey<RefreshFooterState>();

  @override
  void initState() {
    super.initState();
    getList();
  }

  void getList() {
    Provider.of<ReportProvider>(context,listen: false).getReportList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 10, left: 15, right: 15),
      child: Consumer<ReportProvider>(
        builder: (context,data,_) {
          if (data.reportListData.length>0) {
            return Container(
              child: EasyRefresh(
                child: ListView.builder(
                    itemCount: data.reportListData.length,
                    itemBuilder: (context,index) {
                      return _listReportWidget(data.reportListData,index);
                    }
                ),
                onRefresh: () {
                  Provider.of<ReportProvider>(context,listen: false).getReportList(context);
                },
                refreshHeader: ClassicsHeader(
                    key: _headerKey44,
                    refreshText: "下拉刷新数据",
                    refreshReadyText: "准备刷新数据",
                    refreshingText: "数据刷新中...",
                    refreshedText: "刷新完成",
                    bgColor: Colors.pink,
                    textColor: Colors.white,
                    moreInfoColor: Colors.white,
                    refreshHeight: 70.0,
                    isFloat: false,
                    showMore: false,
                    moreInfo: "Updated at %T"
                ),
                refreshFooter: ClassicsFooter(
                    key: _footerKey44,
                    bgColor: Colors.white,
                    textColor: Colors.pink,
                    moreInfoColor: Colors.pink,
                    showMore: true,
                    noMoreText: '没有更多了',
                    moreInfo: '加载中',
                    loadReadyText: '上拉加载'
                ),
                loadMore: () {
                  Provider.of<ReportProvider>(context,listen: false).getReportMoreList(context);
                }
              ),
            );

          } else {
            return Center(
              child: Text('请稍等，数据加载中或暂无数据'),
            );
          }
        },
      ),
    );
  }

  Widget _listReportWidget (List reportList,int index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return ReportDetail(reportList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1,color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))
        ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(reportList,index),
            _rightContent(reportList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(reportList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,

          ),
        ],
      ),
    );
  }

  Widget _rightContent(reportList,index) {
    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '举报描述:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${reportList[index].description}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '举报代码:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${reportList[index].code}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '举报备注:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${reportList[index].remark}'),
              ),
            ],
          ),
        ],
      ),
    );
  }


}

